/**
 * \file
 *           tlmRmtCfgDataInfoSetPara.cpp
 * \brief
 *           Implements tlmRmtCfgDataInfoSetPara class for Telematics services
 *           this is the class of SetPara(30) data infomation of remote configuration.
 *
 * \par Author (last changes):
 *          - Zeng Jie
 *          - BUIC, Continental Automotive Holdings Co., Ltd.
 * \par Project Leader:
 *          -
 * \par Responsible Developer:
 *          - Zeng Jie
 *          - BUIC, Continental Automotive Holdings Co., Ltd.
 * \par Responsible Architect:
 *          - Zhang Fa
 *          - BUIC, Continental Automotive Holdings Co., Ltd.
 *
 * \par Project:
 *      DPCA2
 *
 * \par SW-Component:
 *      Telematics
 *
 * \par SW-Package:
 *      Telematics
 *
 * \par SW-Module:
 *      Telematics Application
 *
* \par SW-Architecture Specification:
 *      DPCA2_TELEMATICS_SW_DD_07a_ready.doc
 *
* \par SW-Design Specification:
 *      DPCA2_TLM-RMTCFG_SW_DD_01d_released.doc
 *
 * \par SW-Test Case Specification:
 *
 * \par CM-Info:
 *
 * \note
 *      This is a note.
 *
 * \bug
 *      - there are no known bugs
 *
 * \par Module-History:
 *
 * \verbatim
 * Date                Author					Reason
 * 10 Dec 2013        Zeng Jie              Initial Creation
 * The latest entry at the head of the history list.
 * \endverbatim
 *
 * \par Copyright Notice:
 * Copyright (C) 2012 Continental Automotive Holdings Co., Ltd.
 * Alle Rechte vorbehalten. All Rights Reserved.\n
 * The reproduction, transmission or use of this document or its contents is
 * not permitted without express written authority.\n
 * Offenders will be liable for damages. All rights, including rights created
 * by patent grant or registration of a utility model or design, are reserved.
 *
 */

#include "stdafx.h"
#include "string.h"
#include "tlmRmtCfgDataInfoSetPara.h"
//## operation ParseSrvConfiguration(msgbuf)
#include "msgbuf.h"
//## operation GetParaItem(uint8_t,uint8_t)
#include "tlmRmtCfgElementPara.h"
#include "teleLogTrace.h"


/**
* \brief tlmRmtCfgDataInfoSetPara: this is constructor of tlmRmtCfgDataInfoSetPara class
*
* \note
*
* \param[in]
* - bufptr: pointer to buffer that store the datainfo message
*   size  : size of the datainfo buffer
*
* \return
* - none
*/
tlmRmtCfgDataInfoSetPara::tlmRmtCfgDataInfoSetPara(uint8_t* bufPtr, uint32_t size) : tlmRmtCfgDataInfo(bufPtr, size){
    //#[ operation tlmRmtCfgDataInfoSetPara(uint8_t*,uint32_t)
    //#]
}

/**
* \brief tlmRmtCfgDataInfoSetPara: this is default constructor of tlmRmtCfgDataInfoSetPara class
*
* \note
*
* \param[in]
* - none
*
* \return
* - none
*/
tlmRmtCfgDataInfoSetPara::tlmRmtCfgDataInfoSetPara():tlmRmtCfgDataInfo() {
}

/**
* \brief ~tlmRmtCfgDataInfoSetPara: this is destructor of tlmRmtCfgDataInfoSetPara class 
*
* \note
*
* \param[in]
* - none
*
* \return
* - none
*/
tlmRmtCfgDataInfoSetPara::~tlmRmtCfgDataInfoSetPara() {
}


/**
* \brief GetParaItem: get the parameter element item 
*
* \note
*
* \param[in]
* - srvId: the service ID.
*   paraCode : the code of parameter element
*
* \return
* - none
*
*/
tlmRmtCfgElementPara* tlmRmtCfgDataInfoSetPara::GetParaItem(uint8_t srvId, uint8_t paraCode) {
    //#[ operation GetParaItem(uint8_t,uint8_t)
    tlmRmtCfgElementPara* pItem=NIL;
    
    if(srvId == TLM_RMT_CFG_SERVICE_ID_WEB)
    {
        pItem = mParaWeb.GetItem(paraCode);
    }
    else if(srvId == TLM_RMT_CFG_SERVICE_ID_NAVI)
    {
        pItem = mParaNavi.GetItem(paraCode);
    }  
    else if(srvId == TLM_RMT_CFG_SERVICE_ID_ADMIN)
    {
        pItem = mParaAdmin.GetItem(paraCode);
    }
    else
    {
       
        //LOG_PRINT(DPCA_TELE_MAIN_ERROR,"[TLM_MSGSRV]:<tlmRmtCfgDataInfoSetPara::%s> invalid service id[%d]\n", __FUNCTION__, srvId);

    }
    
    return pItem;
    //#]
}


/**
* \brief ParseInit:This function is used to parse the dataiofo section of setPara(30) message. 
*
* \note
*
* \param[in]
* - none
*
* \return
* - bool_t : true - parse the data info message successfully.
*          false - parse the data info message failde
*
* [Covers: MRN_TLM-RMTCFG_SW_DD_032]
*/
bool_t tlmRmtCfgDataInfoSetPara::ParseInit() {
    //#[ operation ParseInit()
    //before use ParseInit, the mDataInfoBuffer must be set        
    //0x04:web
    //0x05:navi
    //0x08:admin
    //The format for data info of send configuration request message is like below:
    //0x10 0x7D 0x04 0x7C 0x11 0x7C 0x22 0x7C 0x33 0x7C 0x44 0x7D 0x05 0x55 0x7C 0x66
    //
    
    
    bool_t retVal=true;   
    uint8_t value;
    msgbuf tmpBuf;

    LOG_PRINT(DPCA_TELE_MAIN_INFO,"[TLM_MSGSRV]: <tlmRmtCfgDataInfoSetPara::ParseInit> \r\n");
               
     //get first byte, it should be sep "7E"
    mDataInfoBuffer.getValue(value);
    if(value != SEPERATE_LEVEL_1)
    {
        retVal = false;
        LOG_PRINT(DPCA_TELE_MAIN_ERROR,"[TLM_MSGSRV]:<tlmRmtCfgDataInfoSetPara::%s>get invalid seperator [0x%x], should be 0x7E\n", __FUNCTION__, value);
    }         
              
    if(retVal)
    {
        //followed should be element code "0x10"
        mDataInfoBuffer.getValue(value);
        if(TLM_RMT_CFG_DATA_INFO_CODE_SET_CFG_INFO != value)
        {
            retVal = false;
            LOG_PRINT(DPCA_TELE_MAIN_ERROR,"[TLM_MSGSRV]:<tlmRmtCfgDataInfoSetPara::%s>get invalid element code [0x%x]\n", __FUNCTION__, value);
        }
    }   
    
    if(retVal)
    {//here we can process the parameters sections.
        //the followed byte should be sep "7D"
        mDataInfoBuffer.getValue(value);
        if(value != SEPERATE_LEVEL_2)
        {
            retVal = false;
            LOG_PRINT(DPCA_TELE_MAIN_ERROR,"[TLM_MSGSRV]:<tlmRmtCfgDataInfoSetPara::%s>get invalid seperator [0x%x], should be 0x7D\n", __FUNCTION__, value);
            
        }     
    
        if(retVal)
        {      
            //In this loop, every time shall get all parameters of a service, and parse the parameters.
            while(mDataInfoBuffer.GetFiledContent(SEPERATE_LEVEL_2, tmpBuf))
            {//get the message between sep 0x7D, the content is all parameters of a service
                ParseSrvConfiguration(tmpBuf);

            }
        }
    
    
    }
         
    
    return retVal;
    //#]
}

/**
* \brief ParseSrvConfiguration: parse the configuration of one service
*
* \note
*
* \param[in]
* - buf : the message buf object that include the all parameters of one service
*
* \return
* - bool_t : true - parse the service configuration successfully
*          false - parse the content failed
*
* [Covers: MRN_TLM-RMTCFG_SW_DD_033]
*/
bool_t tlmRmtCfgDataInfoSetPara::ParseSrvConfiguration(msgbuf& buf) {
    //#[ operation ParseSrvConfiguration(msgbuf)
    //the format for parameter content is like this.
    //for last service, it may do not have 0x7D.
    //0x04 0x7C 0x11 0x7C 0x22 0x7C 0x33 0x7C 0x44 0x7D
    
    uint8_t value;
    uint8_t srvId;
    msgbuf codeBuf;
    uint8_t code;
    msgbuf valueBuf;
	bool_t retVal=true;
    
    //dump the buffer content;
    //buf.dumpMsg();
    
    buf.reset();
    
    //the first byte is the service id;
    buf.getValue(srvId);
    
    //the followed byte should be sep "0x7C";
    buf.getValue(value);
    if(value != SEPERATE_LEVEL_3)
    {
        retVal = false;
        //LOG_PRINT(DPCA_TELE_MAIN_ERROR,"[TLM_MSGSRV]:<tlmRmtCfgDataInfoSetPara::%s>get invalid seperator [0x%x], should be 0x7C\n", __FUNCTION__, value);

    }
    
    //this loop get code and value pair of each parameters element.
    while(retVal && (!buf.IsReachEnd()))
    {
		//codeBuf.reset();
		code=0;
		valueBuf.reset();
        //get the element code of the parameter
        //buf.GetFiledContent(SEPERATE_LEVEL_3, codeBuf);
        //TODO:USE this to get the code value
        buf.getValue(code);
    
        //followed is sep 0x7C
        //buf.getValue(value);
        //if(value != SEPERATE_LEVEL_3)
        //{
        //    retVal = false;
        //    //LOG_PRINT(DPCA_TLM_RMT_CFG_ERROR,"[TLMRMTCFGMENU]: get invalid seperator [], should be 0x7C\r\n",value);
        //}
    
        //get the value of the parameter
        buf.GetFiledContent(SEPERATE_LEVEL_3, valueBuf);
    
        SetConfiguration(srvId, code, valueBuf);
    }

	return retVal;
    
    
    //#]
}

/**
* \brief SetConfiguration : set the configuration by parsed value
*
* \note
*
* \param[in]
* - srvId: the service ID
*   codeBuf : pointer to the buffer has parameter element code
*   valueBuf : pointer to the buffer has paramenter element value
*
* \return
* - none
*
* [Covers: MRN_TLM-RMTCFG_SW_DD_034]
*/
void tlmRmtCfgDataInfoSetPara::SetConfiguration(uint8_t srvId, msgbuf& codeBuf, msgbuf& valueBuf) {
    //#[ operation SetConfiguration(uint8_t,msgbuf,msgbuf)
    uint8_t value;
    uint8_t code;
    uint8_t data;
    uint32_t index;
    tlmRmtCfgElementPara* pItem=NIL;  
    uint8_t buf[TLM_MAX_BUFFER_SIZE];
    
    codeBuf.getValue(value);
    if(value==ESCAPE_CODE)
    {//this is a escape code, we should check next one
        codeBuf.getValue(value);
        code = TranslateData(value);
    }
    else
    {
        code = value;
    }
    
    memset(buf, 0, sizeof(buf));
    index=0;
         
    while((valueBuf.getValue(value)) && (index < TLM_MAX_BUFFER_SIZE))
    {
        if(value==ESCAPE_CODE)
        {//this is the escape code, read next one
            valueBuf.getValue(value);
            data = TranslateData(value);
        }
        else
        {
            data = value;
        }
        buf[index++] = data;
    }  
        
    pItem = GetParaItem(srvId, code);
    if(pItem!=NIL)  
	{
		pItem->SetValue(buf, index);
	}
    
    //#]
}

/**
* \brief SetConfiguration : set the configuration by parsed value
*
* \note
*
* \param[in]
* - srvId: the service ID
*   codeValue : code value of the element
*   valueBuf : pointer to the buffer has paramenter element value
*
* \return
* - none
*
*/
void tlmRmtCfgDataInfoSetPara::SetConfiguration(uint8_t srvId, uint8_t& codeValue, msgbuf& valueBuf) {
    //#[ operation SetConfiguration(uint8_t,msgbuf,msgbuf)
    uint8_t value;
    uint8_t code;
    uint8_t data;
    uint32_t index;
    tlmRmtCfgElementPara* pItem=NIL;  
    uint8_t buf[TLM_MAX_BUFFER_SIZE];
    
    
    code = codeValue;
    
    
    memset(buf, 0, sizeof(buf));
    index=0;
         
    while((valueBuf.getValue(value)) && (index < TLM_MAX_BUFFER_SIZE))
    {
        if(value==ESCAPE_CODE)
        {//this is the escape code, read next one
            valueBuf.getValue(value);
            data = TranslateData(value);
        }
        else
        {
            data = value;
        }
        buf[index++] = data;
    }  
        
    pItem = GetParaItem(srvId, code);
    if(pItem!=NIL)  
	{
		pItem->SetValue(buf, index);
	}
    
    //#]
}

